#include "albumtopwidget.h"

AlbumTopWidget::AlbumTopWidget(QWidget *parent)
    : QWidget{parent}
{
    albumpicturelabel=new QLabel(this);
    albumplaynumlabel=new QPushButton(albumpicturelabel);
    albumtitlelabel=new QLabel(this);
    taglabel=new QLabel("标签:",this);
    multitagslabel=new AdaptiveMultiLabel(this);
    createdatelabel=new QLabel(this);
    introline=new QLineEdit(this);
    introtextedit=new PopupTextEdit(this);
    searchline=new TelescopicLineEdit(this);
    creatorheadshotlabel=new ClickedLabel(this);
    creatornamelabel=new ClickedLabel(this);
    introdetaillabel=new ClickedLabel(this);
    setplayallbtn=new QPushButton("播放全部",this);
    setfavorbtn=new QPushButton("",this);
    setdownloadbtn=new QPushButton("下载",this);
    setmorebtn=new QPushButton("...",this);
    albumsonglabel=new SmallPrintLabel(this);
    albumcommentlabel=new SmallPrintLabel(this);
    albumcollectorlabel=new SmallPrintLabel(this);

    creatorlayout=new QHBoxLayout;
    setlayout=new QHBoxLayout;
    albuminfolayout=new QHBoxLayout;
    introlayout=new QHBoxLayout;
    hmain=new QHBoxLayout;

    vmain=new QVBoxLayout(this);
    leftlayout=new QVBoxLayout;
    rightlayout=new QVBoxLayout;

    introlayout->addWidget(introline);
    introlayout->addWidget(introdetaillabel);
    introlayout->addStretch(1);

    creatorlayout->addWidget(creatorheadshotlabel);
    creatorlayout->addWidget(creatornamelabel);
    //暂时隐藏
    creatorheadshotlabel->hide();
    creatornamelabel->hide();
    creatorlayout->addWidget(taglabel);
    creatorlayout->addWidget(multitagslabel);
    creatorlayout->addWidget(createdatelabel);
    creatorlayout->addStretch(1);

    introdetaillabel->setText("详情");
    introdetaillabel->setStyleSheet("color:blue");
    introdetaillabel->setCursor(Qt::PointingHandCursor);
    connect(introdetaillabel,&ClickedLabel::clicked,this,&AlbumTopWidget::onClickIntroDetailLabel);

    setlayout->addWidget(setplayallbtn);
    setlayout->addWidget(setfavorbtn);
    setlayout->addWidget(setdownloadbtn);
    setlayout->addWidget(setmorebtn);
    setlayout->addStretch(1);

    albuminfolayout->addWidget(albumsonglabel);
    albuminfolayout->addWidget(albumcommentlabel);
    albuminfolayout->addWidget(albumcollectorlabel);
    albuminfolayout->addStretch(1);
    albuminfolayout->addWidget(searchline);
    albuminfolayout->setSpacing(30);

    leftlayout->addWidget(albumpicturelabel);
    leftlayout->addStretch(1);

    rightlayout->addWidget(albumtitlelabel);
    rightlayout->addLayout(introlayout);
    rightlayout->addLayout(creatorlayout);
    rightlayout->addStretch(1);
    rightlayout->addLayout(setlayout);
    rightlayout->setSpacing(10);

    hmain->addLayout(leftlayout);
    hmain->addLayout(rightlayout);
    hmain->setSpacing(25);

    vmain->addLayout(hmain);
    vmain->addLayout(albuminfolayout);

    //设置各部件大小
    albumpicturelabel->setFixedSize(175,175);

    albumplaynumlabel->setGeometry(100,5,75,20);
    albumplaynumlabel->setStyleSheet("color:white;background:transparent;");
    albumplaynumlabel->setIcon(QIcon(":/icon/playnum.png"));

    albumtitlelabel->setFixedSize(500,20);

    introline->setFixedHeight(20);
    introline->setStyleSheet("border:none;background:transparent;color:grey;");
    introline->setReadOnly(true);

    introtextedit->setReadOnly(true);
    introtextedit->setStyleSheet("border:none;background:#f0f3f6;color:grey;");

    albumsonglabel->setText("歌曲");
    albumcommentlabel->setText("评论");
    albumcollectorlabel->setText("收藏者");
    albuminfolayout->setSpacing(10);

    creatorheadshotlabel->setFixedSize(20,20);
    creatorheadshotlabel->setStyleSheet("border-radius:10px;background:black;");

    creatornamelabel->setStyleSheet("color:grey");

    taglabel->setStyleSheet("color:grey");

    multitagslabel->setStyleSheet("color:blue;background:transparent");
    multitagslabel->setCursor(Qt::PointingHandCursor);

    createdatelabel->setStyleSheet("color:grey;");

    setplayallbtn->setIcon(QIcon(":/icon/play.png"));
    setplayallbtn->setStyleSheet("QPushButton{background:#ea3e3e;"
                                 "border:none;"
                                 "border-radius:10px;"
                                 "color:black;}"
                                 "QPushButton:hover{background:#c92c2c;}");
    setplayallbtn->setFixedSize(90,35);
    connect(setplayallbtn,&QPushButton::clicked,[this]{emit playall();});//发出播放全部的信号

    setfavorbtn->setIcon(QIcon(":/icon/favorite.png"));
    setfavorbtn->setStyleSheet("QPushButton{background:#e4e8ec;"
                                 "border:none;"
                                 "border-radius:10px;"
                                 "color:black;}"
                                 "QPushButton:hover{background:#c3c7cb;}");
    setfavorbtn->setFixedSize(90,35);
    connect(setfavorbtn,&QPushButton::clicked,[this]{emit favor();});

    setdownloadbtn->setIcon(QIcon(":/icon/download.png"));
    setdownloadbtn->setStyleSheet("QPushButton{background:#e4e8ec;"
                               "border:none;"
                               "border-radius:10px;"
                               "color:black;}"
                               "QPushButton:hover{background:#c3c7cb;}");
    setdownloadbtn->setFixedSize(90,35);
    connect(setdownloadbtn,&QPushButton::clicked,[this]{emit download();});

    setmorebtn->setStyleSheet("QPushButton{background:#e4e8ec;"
                               "border:none;"
                               "border-radius:10px;"
                               "color:black;}"
                               "QPushButton:hover{background:#c3c7cb;}");
    setmorebtn->setFixedSize(45,35);
    connect(setmorebtn,&QPushButton::clicked,[this]{emit more(mapToGlobal(setmorebtn->geometry().bottomRight()));});

    searchline->setFixedSize(75,30);
    searchline->SetShrinkWidth(75);
    searchline->SetExpandWidth(200);
    searchline->setPlaceholderText("搜索");
    searchline->addAction(QIcon(":/icon/search.png"),QLineEdit::LeadingPosition);
    searchline->setClearButtonEnabled(true);
    searchline->setStyleSheet("border:none;"
                              "border-radius:10px;"
                              "background:white;");
    connect(searchline,&QLineEdit::textEdited,this,[this](const QString&text){
        emit textEdited(text);
    });

    albumsonglabel->setFixedHeight(25);
    albumsonglabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    albumsonglabel->SetFlag(true);
    connect(albumsonglabel,&SmallPrintLabel::clicked,this,&AlbumTopWidget::onClickAlbumBottomLabel);

    albumcommentlabel->setFixedHeight(25);
    albumcommentlabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    albumcommentlabel->SetFlag(false);
    connect(albumcommentlabel,&SmallPrintLabel::clicked,this,&AlbumTopWidget::onClickAlbumBottomLabel);

    albumcollectorlabel->setFixedHeight(25);
    albumcollectorlabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    albumcollectorlabel->SetFlag(false);
    connect(albumcollectorlabel,&SmallPrintLabel::clicked,this,&AlbumTopWidget::onClickAlbumBottomLabel);

    SetFont();

}

void AlbumTopWidget::SetAlbumPicture(const QString&filepath)
{
    QImage image(filepath);
    QPixmap pixmap=QPixmap::fromImage(image);
    pixmap=pixmap.scaled(albumpicturelabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    albumpicturelabel->setPixmap(SongItem::paintPictureRoundRect(pixmap,10));
}
void AlbumTopWidget::SetAlbumPicture(const QByteArray&data)
{
    QImage image(data);
    QPixmap pixmap=QPixmap::fromImage(image);
    pixmap=pixmap.scaled(albumpicturelabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    albumpicturelabel->setPixmap(SongItem::paintPictureRoundRect(pixmap,10));
}

void AlbumTopWidget::SetAlbumPicture(const QPixmap&picture)
{
    QPixmap pixmap;
    pixmap=picture.scaled(albumpicturelabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    albumpicturelabel->setPixmap(SongItem::paintPictureRoundRect(pixmap,10));
}

void AlbumTopWidget::SetAlbumPicture(const QImage&picture)
{
    QPixmap pixmap=QPixmap::fromImage(picture);
    SetAlbumPicture(pixmap);
}

void AlbumTopWidget::SetAlbumInfo(qint32 playnum,const QString&albumtitle,const QString&intro)
{
    double num;
    if(playnum>10000)
    {
        num=1.0*playnum/10000;
        albumplaynumlabel->setText(QString::number(num,'f',2)+"万");
    }
    else if(playnum>100000000)
    {
        num=1.0*playnum/100000000;
        albumplaynumlabel->setText(QString::number(num,'f',2)+"亿");
    }
    else
        albumplaynumlabel->setText(QString::number(playnum));

    albumtitlelabel->setText(albumtitle);
    introline->setText(intro);
    introduction=intro;
}

void AlbumTopWidget::SetCreator(const QString&filepath,const QString&name,const QStringList&tags,const QDate& date)
{
    QImage image(filepath);
    QPixmap pixmap=QPixmap::fromImage(image);
    pixmap=pixmap.scaled(QSize(20,20),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    pixmap=SongItem::paintPictureRoundRect(pixmap,10);
    creatorheadshotlabel->setPixmap(pixmap);

    QFontMetrics metrics(creatornamelabel->font());
    auto namerect=metrics.boundingRect(name);
    creatornamelabel->setFixedSize(namerect.width()+10,namerect.height());
    creatornamelabel->setText(name);

    multitagslabel->SetStringList(tags);
    multitagslabel->SetAdaptiveSize();

    auto daterect=metrics.boundingRect(date.toString("yyyy-MM-dd")+"创建");
    createdatelabel->setFixedSize(daterect.width()+10,daterect.height());
    createdatelabel->setText(date.toString("yyyy-MM-dd")+"创建");
}

void AlbumTopWidget::SetCreator(const QByteArray&data,const QString&name,const QStringList&tags,const QDate& date)
{
    QImage image(data);
    QPixmap pixmap=QPixmap::fromImage(image);
    pixmap=pixmap.scaled(QSize(20,20),Qt::KeepAspectRatio,Qt::SmoothTransformation);
    pixmap=SongItem::paintPictureRoundRect(pixmap,10);
    creatorheadshotlabel->setPixmap(pixmap);

    QFontMetrics metrics(creatornamelabel->font());
    auto namerect=metrics.boundingRect(name);
    creatornamelabel->setFixedSize(namerect.width()+10,namerect.height());

    multitagslabel->SetStringList(tags);
    multitagslabel->SetAdaptiveSize();

    auto daterect=metrics.boundingRect(date.toString("yyyy-MM-dd")+"创建");
    createdatelabel->setFixedSize(daterect.width()+10,daterect.height());
    createdatelabel->setText(date.toString("yyyy-MM-dd")+"创建");
}

void AlbumTopWidget::onClickAlbumBottomLabel()
{
    SmallPrintLabel* label=qobject_cast<SmallPrintLabel*>(sender());
    label->SetFlag(true);
    if(label==albumsonglabel)
    {
        albumcommentlabel->SetFlag(false);
        albumcollectorlabel->SetFlag(false);
        emit showSongItem();
    }
    else if(albumcommentlabel==label)
    {
        albumcollectorlabel->SetFlag(false);
        albumsonglabel->SetFlag(false);
        emit showComment();
    }
    else
    {
        albumsonglabel->SetFlag(false);
        albumcommentlabel->SetFlag(false);
        emit showFavor();
    }
}

void AlbumTopWidget::onClickIntroDetailLabel()
{
    introtextedit->setText(introduction);
    qint32 width=rect().width()-introline->x();
    qint32 height=albumpicturelabel->y()+albumpicturelabel->height()-introline->y();
    QPoint point=mapToGlobal(introline->pos());
    introtextedit->setGeometry(point.x(),point.y(),width,height);//专辑简介显示
    introtextedit->show();
}

void AlbumTopWidget::resizeEvent(QResizeEvent*e)
{
    QWidget::resizeEvent(e);
    QFontMetrics metrics(introline->font());
    QRect rect=metrics.boundingRect(introduction);
    qint32 nowwidth=e->size().width()-introline->geometry().x()-50;
    auto list=introlayout->children();
    if(nowwidth>=rect.width())//够显示所有文本时
    {
        introline->setText(introduction);
        introline->setFixedWidth(rect.width()+20);
        if(introlayout->indexOf(introdetaillabel)!=-1)//已添加到introlayout时
        {
            introlayout->removeWidget(introdetaillabel);
            introdetaillabel->hide();
        }
    }
    else//不够时
    {
        introline->setFixedWidth(nowwidth+20);
        QString newstring=metrics.elidedText(introduction,Qt::ElideRight,nowwidth);
        introline->setText(newstring);
        if(introlayout->indexOf(introdetaillabel)==-1)//未添加
        {
            introlayout->addWidget(introdetaillabel);
            introdetaillabel->show();
        }
    }
}

void AlbumTopWidget::SetFont()
{
    albumplaynumlabel->setFont(QFont("Microsoft YaHei",10,INT_MAX));
    albumtitlelabel->setFont(QFont("Microsoft YaHei",16));
    taglabel->setFont(QFont("Microsoft YaHei",9));
    multitagslabel->setFont(QFont("Microsoft YaHei",9));
    introline->setFont(QFont("Microsoft YaHei",10));
    introtextedit->setFont(QFont("Microsoft YaHei",10));
    searchline->setFont(QFont("Microsoft YaHei",11));
    creatornamelabel->setFont(QFont("Microsoft YaHei",9));
    introdetaillabel->setFont(QFont("Microsoft YaHei"));
    setplayallbtn->setFont(QFont("Microsoft YaHei",10));
    setfavorbtn->setFont(QFont("Microsoft YaHei",10));
    setdownloadbtn->setFont(QFont("Microsoft YaHei",10));
    albumsonglabel->setFont(QFont("Microsoft YaHei",11,INT_MAX>>20));
    albumcommentlabel->setFont(QFont("Microsoft YaHei",11,INT_MAX>>20));
    albumcollectorlabel->setFont(QFont("Microsoft YaHei",11,INT_MAX>>20));
}

TelescopicLineEdit::TelescopicLineEdit(QWidget*parent):QLineEdit(parent),expandwidth(0),shrinkwidth(0)
{
    expandanimation = new QPropertyAnimation(this, "width");
    expandanimation->setDuration(300);
    expandanimation->setEasingCurve(QEasingCurve::InOutQuad);//设置运动曲线

    // 创建缩短动画
    shrinkanimation = new QPropertyAnimation(this, "width");
    shrinkanimation->setDuration(300); // 动画持续时间：300 毫秒
    shrinkanimation->setEasingCurve(QEasingCurve::InOutQuad);
}

void TelescopicLineEdit::SetExpandWidth(const int width)
{
    expandwidth=width;
}

void TelescopicLineEdit::SetShrinkWidth(const int width)
{
    shrinkwidth=width;
    setFixedWidth(width);
}

void TelescopicLineEdit::focusInEvent(QFocusEvent*e)
{
    QLineEdit::focusInEvent(e);
    expandanimation->setStartValue(shrinkwidth);
    expandanimation->setEndValue(expandwidth);
    expandanimation->start();

}

void TelescopicLineEdit::focusOutEvent(QFocusEvent*e)
{
    QLineEdit::focusOutEvent(e);
    shrinkanimation->setStartValue(expandwidth); // 当前宽度
    shrinkanimation->setEndValue(shrinkwidth);   // 缩短后的宽度
    shrinkanimation->start();
}

